#include "s3e.h"
#include "Iw2D.h"
#include <list>
#include <vector>
#include "ui_menu.h"
#include "ui_page.h"
#include "ui_widgets.h"

namespace UI
{
	
Menu::Menu()
	: m_CurrentPage(NULL)
	, m_TouchStart(CIwSVec2(0, 0))
{

}

Menu::~Menu()
{
	for (uint i = 0; i < m_Pages.size(); ++i)
	{
		delete m_Pages[i];
	}
}

void Menu::Update( float a_DT )
{
	if (s3ePointerGetTouchState(0) & S3E_POINTER_STATE_PRESSED)
	{
		m_TouchStart = CIwSVec2(s3ePointerGetTouchX(0), s3ePointerGetTouchY(0));
	}

	if (m_CurrentPage != NULL)
	{
		m_CurrentPage->Update(a_DT);
	}
}

void Menu::Render()
{
	if (m_CurrentPage != NULL)
	{
		m_CurrentPage->Render();
	}
}

void Menu::Delete()
{
	for (uint i = 0; i < m_Pages.size(); ++i)
	{
		m_Pages[i]->Delete();
	}

	for (int i = m_Pages.size() - 1; i >= 0; --i)
	{
		UI::Page* page = m_Pages[i];
		m_Pages.pop_back();
		delete page;
	}
	m_Pages.clear();

	//delete this;
}

void Menu::SetPage( const char* a_Name )
{
	for (uint i = 0; i < m_Pages.size(); ++i)
	{
		if ( m_Pages[i]->GetName() == a_Name)
		{
			m_CurrentPage = m_Pages[i];
		}
	}
}

const char* Menu::GetCurrentPageName()
{
	if (m_CurrentPage != NULL)
	{
		return m_CurrentPage->GetName();
	}
	
	return "(no page set)";
}

void Menu::AddPage( UI::Page* a_Page )
{
	a_Page->SetParentMenu(this);
	m_Pages.push_back(a_Page);

	// If no current page has been set yet, set this new page as the current one.
	if (m_CurrentPage == NULL)
	{
		m_CurrentPage = a_Page;
	}
}

CIwSVec2 Menu::GetTouchStart()
{
	return m_TouchStart;
}

}